import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginView from "@/views/LoginView.vue";
import UserIndex from "@/views/UserIndex.vue";
import store from "@/store";
import RegisterView from "@/views/RegisterView.vue";
import UserInfo from "@/views/UserInfo.vue";

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'loginview',
    component: LoginView
  },
    {
      path: '/userindex',
      name: 'userindex',
      component:UserIndex,
      children:[
        {
          path: 'userinfo',
          name: 'userinfo',
          component: UserInfo,
        },
      ]
    },
    {
      path: '/register',
      name: 'register',
      component:RegisterView
    },



]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/' || to.path === '/register') {
     next();
  } else {
    if (store.state.token == null || store.state.token === '') {
      next('/login');
    } else {
      next();
    }
  }
});
export default router
